////////////////////////////////////////////////////////////////////////////////
//
//  FLEX USER GROUP HOKURIKU
//  Copyright 2009 Flex User Group Hokuriku
//  All Rights Reserved.
// 
//  NOTICE: Flex User Group Hokuriku permits you to use, 
//  modify, and distribute this file 
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package net.fxug.hokuriku.chocolat.helper
{
    import flash.events.Event;
    import flash.events.SQLErrorEvent;

    import mx.core.IMXMLObject;
    import mx.events.FlexEvent;

    import net.fxug.hokuriku.chocolat.dao.DatabaseManager;
    import net.fxug.hokuriku.chocolat.dao.VoteDao;
    import net.fxug.hokuriku.chocolat.events.SQLVoteEvent;
    import net.fxug.hokuriku.chocolat.events.SessionTimerEvent;
    import net.fxug.hokuriku.chocolat.events.ViewChangeEvent;
    import net.fxug.hokuriku.chocolat.model.ApplicationDataModel;
    import net.fxug.hokuriku.chocolat.service.VoteManager;
    import net.fxug.hokuriku.chocolat.utils.FilterUtil;
    import net.fxug.hokuriku.chocolat.view.SessionTimeUp;
    import net.fxug.hokuriku.chocolat.view.SpeakerView;
    import net.fxug.hokuriku.chocolat.view.TimelineTimer;

    /**
     * スピーカー画面のヘルパークラス
     *
     * @author shoito
     */
    public class SpeakerViewHelper implements IMXMLObject
    {
        private var view:SpeakerView;
        private var sessionTimeUp:SessionTimeUp;

        public function initialized(document:Object, id:String):void
        {
            this.view = document as SpeakerView;
            this.view.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
            this.view.filters = [FilterUtil.getDropShadowFilter()];
        }

        private function creationCompleteHandler(event:FlexEvent):void
        {
            this.view.setting.addEventListener(SessionTimerEvent.START, setting_timerStartHandler);
            this.view.setting.addEventListener(ViewChangeEvent.BACK, setting_backHandler);
        }

        private function setting_timerStartHandler(event:Event):void
        {
            this.view.currentState = "timer";
            this.view.timer.addEventListener(SessionTimerEvent.TIME_UP, timer_timeUpHandler);
            this.view.dispatchEvent(new ViewChangeEvent(ViewChangeEvent.HIDE));
        }

        private function setting_backHandler(event:Event):void
        {
            this.view.dispatchEvent(new ViewChangeEvent(ViewChangeEvent.BACK));
        }

        private function timer_timeUpHandler(event:Event):void
        {
            if (this.view.stage.nativeWindow.closed)
            {
                return;
            }

            sessionTimeUp = new SessionTimeUp();
            sessionTimeUp.addEventListener(Event.CLOSE, timeUpWindow_closeHandler);
            sessionTimeUp.visible = true;

            this.view.currentState = "score";
        }

        private function timeUpWindow_closeHandler(event:Event):void
        {
//            DatabaseManager.getInstance().addEventListener(SQLVoteEvent.SELECT_VOTE_BY_SESSIONID_EVENT, dbManager_selectVoteBySessionHandler);
//            DatabaseManager.getInstance().addEventListener(SQLErrorEvent.ERROR, dbManager_errorHandler);
//            DatabaseManager.getInstance().findVoteBySessionId(ApplicationDataModel.getInstance().sessionId);
//        }
//
//        private function dbManager_selectVoteBySessionHandler(sqlVoteEvent:SQLVoteEvent):void
//        {
//            var ary:Array = sqlVoteEvent.data as Array;
//
//            if (ary == null || ary.length == 0)
//            {
//                viewChange();
//                return;
//            }
//
//            var heCount:int = 0;
//            var boCount:int = 0;
//            var index:int = ary.length;
//            for (var i:int = 0; i < index; i++)
//            {
//                var voteDao:VoteDao = ary[i];
//                voteDao.protocol ? heCount++ : boCount++;
//            }
//
//            ApplicationDataModel.getInstance().goodComment = heCount + "へぇ";
//            ApplicationDataModel.getInstance().badComment = boCount + "ぶぅ";
//            ApplicationDataModel.getInstance().feedback = this.createCongratulationMessage(heCount, boCount);
//
            viewChange();
        }

//        private function dbManager_errorHandler(event:SQLErrorEvent):void
//        {
//            viewChange();
//        }
//
//        private function createCongratulationMessage(good:int, bad:int):String
//        {
//            return good - bad > 0 ? "あなたのスピーカーとしての腕はスティーブ・ジョブス並みと言えるでしょう。" : "たいへんよく出来ました。"
//        }

        private function viewChange():void
        {
            this.view.dispatchEvent(new ViewChangeEvent(ViewChangeEvent.SHOW));
        }
    }
}